/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minimalshift;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Michal
 */
public class FileReader {

	private int intervalLen;
	private List<Integer> reqs = new LinkedList<>();
	private List<Shift> shifts = new LinkedList<>();

	public FileReader(String path) {
		proceedFile(path);
	}

	public int getIntervalLen() {
		return intervalLen;
	}

	public List<Integer> getReqs() {
		return reqs;
	}

	public List<Shift> getShifts() {
		return shifts;
	}

	private void proceedFile(String path) {
		try {
			BufferedReader r = new BufferedReader(new java.io.FileReader(path));

			//Delka cyklu
			intervalLen = Integer.parseInt(readLine(r));

			//Dny v cyklu - preskocime
			readLine(r);
			//Cyklicke - preskocime a ocekavame ze vzdy je
			readLine(r);

			//Pozadavky
			String[] requirements = readLine(r).split(" ");
			for (String s : requirements) {
				reqs.add(Integer.parseInt(s));
			}

			//Nacteme smeny
			int count = Integer.parseInt(readLine(r));
			List<RawShift> rShifts = new LinkedList<>();
			for (int i = 0; i < count; i++) {
				String[] s = readLine(r).split(" ");
				String[] t = s[2].split(":");
				RawShift rs = new RawShift();
				int a = Integer.parseInt(t[0]) * 60;
				a += Integer.parseInt(t[1]);
				rs.setStart(a);
				rs.setStartAfter(Integer.parseInt(s[3]));
				rs.setStartBefore(Integer.parseInt(s[4]));
				rs.setLenght(Integer.parseInt(s[5]));
				rs.setLenghtAfter(Integer.parseInt(s[6]));
				rs.setLenghtBefore(Integer.parseInt(s[7]));
				rShifts.add(rs);
			}
			processRawShifts(rShifts);
			r.close();

		} catch (FileNotFoundException ex) {
			System.out.println("Prusvih cislo 1");
		} catch (IOException ex) {
			System.out.println("Prusvih cislo 3");
		}
	}

	private String readLine(BufferedReader r) {
		try {
			String line = r.readLine();

			if (line.startsWith("#")) {
				return readLine(r);
			}
			if (line.isEmpty()) {
				return readLine(r);
			}
			return line.trim();
		} catch (IOException ex) {
			System.out.println("Prusvih cislo 2");
		}
		return null;
	}

	private void processRawShifts(List<RawShift> rShifts) {
		for (RawShift shift : rShifts) {
			int minStart = shift.getStart() - shift.getStartBefore();
			int maxStart = shift.getStart() + shift.getStartAfter();
			int minLen = shift.getLenght() - shift.getLenghtBefore();
			int maxLen = shift.getLenght() + shift.getLenghtBefore();
			int posShiftNum = 1 + (maxStart - minStart) / intervalLen;
			int posLenNum = 1 + (maxLen - minLen) / intervalLen;

			//proiterujem vsechny mozny zacatky
			for (int i = 0; i < posShiftNum; i++) {
				//proiterujem vsechny delky
				for (int j = 0; j < posLenNum; j++) {
                    Shift s = new Shift(minStart + i * intervalLen, minLen + j * intervalLen);
					//pridame vsechny mozny smeny
					shifts.add(s);
				}
			}
		}
	}
}
